package com.tedu.order.controller;

import com.tedu.common.response.JsonResult;
import com.tedu.order.pojo.dto.OrderQuery;
import com.tedu.order.pojo.dto.OrderSavaParam;
import com.tedu.order.pojo.entity.Order;
import com.tedu.order.pojo.vo.OrderVO;
import com.tedu.order.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(tags = "订单模块")
@Slf4j
@RestController
@RequestMapping("/api/order")
public class OrderController {
    @Autowired
    OrderService orderService;
    @ApiOperation("查询订单")
    @GetMapping("select")
    public JsonResult selectOrder(OrderQuery query){
        log.debug("查询订单列表:orderQuery={}",query);
        List<OrderVO> list = orderService.selectOrder(query);
        return JsonResult.ok(list);
    }
    @ApiOperation("创建订单")
    @PostMapping("create")
    public JsonResult saveOrder(OrderSavaParam saveParam){

        Order order = new Order();
        BeanUtils.copyProperties(saveParam,order);
        if (order.getOrderId()==null){
            log.debug("创建订单列表:OrderSaveParam={}",saveParam);
            orderService.create(order);
        }else {
            log.debug("更新订单列表:OrderSaveParam={}",saveParam);
            orderService.update(order);
        }
        return JsonResult.ok();
    }
    @ApiOperation("删除订单")
    @PostMapping("delete/{id}")
    public JsonResult deleteOrderById(@PathVariable Long id){
        log.debug("删除订单:deleteOrderById={}",id);
        orderService.deleteById(id);
        return JsonResult.ok();
    }

}
